package com.ely.coupon.activity.validation.constraint;

import com.ely.coupon.activity.validation.CheckType;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

public class CheckTypeValidator implements ConstraintValidator<CheckType, Integer> {
    private int[] types;

    @Override
    public void initialize(CheckType constraintAnnotation) {
        this.types = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return Arrays.stream(types).filter(num -> value == num).findAny().isPresent();
    }
}
